using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class TemplateAddEdit : AdminBaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// event BindTemplate display content template with descript id;
        /// Event called by TemplateList 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BindTemplate(object sender, EventArgs e)
        {
            TemplateList tempList = (TemplateList)sender;
            long templateID = Convert.ToInt64(tempList.SelectedID);
            ddlClassPage.DataBind();
            
            ddlClassPage.Enabled = false;
            ddlType.DataBind();

           
            Model.ITemplate template = TemplateController.GetTemplate(templateID);
            if (template != null)
            {
                tbName.Text = template.Name;
                tbName.ReadOnly = true;
                ViewState["template"] = template;
                tbContent.Text = template.TemplateContent;
                tbDescription.Text = template.Description;
                if(ddlType.SelectedItem != null)
                {
                    ddlType.SelectedValue = ((int)template.Type).ToString();
                }
                ///Tuong changed
                ddlClassPage.SelectedValue = Convert.ToString(template.ClassID);
                ucCmsExtPropertiesForm.PropertyList = template.ExtPropertyList;

                ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassPage.SelectedValue));                
                if (ucCmsExtPropertiesForm.PropertyList.Count > 4) fsExt.Visible = true;
                else fsExt.Visible = false;

            }
            lblStatus.Text = "View Template";
        }
        
        public void DoSaveTemplate(object sender, EventArgs e)
        {
            Model.ITemplate template = GetTemplate();
            string status = "";
            if (template != null)
            {
                if (template.ID > 0)
                {
                    template.EditedBy = IGlobalistMembership.GetUser().ID;
                    TemplateController.UpdateTemplate(template);
                    status = "Save successful";
                }
                else
                {
                    TemplateController.InsertTemplate(template);
                    ViewState["template"] = template;
                    status = "Add New Successful";
                }                
            }
            lblStatus.Text = status;
        }

        private Model.ITemplate GetTemplate()
        {
            Model.ITemplate template = new Model.Template();
            if (String.IsNullOrEmpty(tbName.Text) && String.IsNullOrEmpty(tbDescription.Text) )
            {
                return null;
            }
            if (String.IsNullOrEmpty(tbContent.Text))
            {
                return null;
            }
            IList<Model.ITemplate> templateList = TemplateController.GetTemplateListByClass(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
            if (templateList.Count > 0)
            {
                foreach (Model.ITemplate templ in templateList)
                {
                    if (templ.Name.Equals(tbName.Text.Replace(" ", "")) && ViewState["template"] == null)
                    {
                        return null;
                    }
                }
            }
            if (ViewState["template"] != null)
            {
                template = (Model.ITemplate)ViewState["template"];
            }
            template.Description = tbDescription.Text;
            template.ClassID = Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]);
            template.ClassTypeID =Convert.ToInt16(CmsObjectType.Template);
            template.ExtPropertyList = ucCmsExtPropertiesForm.PropertyList;
            template.TemplateContent = tbContent.Text;
            template.Name = tbName.Text.Replace(" ","");
            template.Type = (TemplateType)Enum.Parse(typeof(TemplateType), ddlType.SelectedValue);
            template.Author = IGlobalistMembership.GetUser().ID;
            return template;
            
        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;
        }
        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
        }

        public void DoNewTemplate(object sender, EventArgs e)
        {
            ViewState["template"] = null;
            tbDescription.Text = "";
            ddlClassPage.Enabled = true;
            ddlClassPage.DataBind();            
            tbName.Text = "";
            tbName.Focus();
            tbName.ReadOnly = false;
            tbContent.Text = "";
            lblStatus.Text = "Add New Template ";
            
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassPage.SelectedValue));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 4) fsExt.Visible = true;
            else fsExt.Visible = false;
            

        }

        public void DoCopyTemplate(object sender, EventArgs e)
        {
            if (ViewState["template"] != null)
            {
                Model.ITemplate template = (Model.ITemplate)ViewState["templateID"];
                if (template != null)
                {
                    tbContent.Text = template.TemplateContent;
                    tbDescription.Text = template.Description;
                    tbName.Text = template.Name;
                    ddlType.SelectedValue = template.Type.ToString();
                    ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassPage.SelectedValue));                    
                    ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
                    
                }
            }
        }
        public void DoDeleteTemplate(object sender, EventArgs e)
        {
            
        }

        protected void ddlClassPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session[SessionVariable.Clazz.CurrentID] = ddlClassPage.SelectedValue;
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClassPage.SelectedValue));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count >4) fsExt.Visible = true;            
            else fsExt.Visible = false;
        }
    }
}